package edu.school.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.school.entity.BigData;
import edu.school.entity.User;
import edu.school.entity.UserExample;
import edu.school.mapper.UserMapper;
import edu.school.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public Boolean registe(User user) {
        return userMapper.insertSelective(user)>0?true:false;
    }

    @Override
    public PageInfo<User> getPageList(Integer pageNum, Integer pageSize, User record) {
        UserExample example = new UserExample();

        UserExample.Criteria criteria = example.createCriteria();
        if (null != record) {

            if (StringUtils.isNoneBlank(record.getStuno())) { //根据学号查询
                criteria.andStunoLike("%" + record.getStuno() + "%");
            }
            if (StringUtils.isNoneBlank(record.getNick_name())) { //根据内容查询
                criteria.andNick_nameLike("%" + record.getNick_name() + "%");
            }
            //关键步骤：分页查询的步骤
        }
        PageHelper.startPage(pageNum, pageSize);//第1步
        List<User> list = userMapper.selectByExample(example);//第2步

        PageInfo<User> info = new PageInfo<>(list);
        info.setPageNum(info.getPageNum());
        info.setPages(info.getPages());
        return info;
    }

    @Override
    public User findById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean update(User user) {
        return userMapper.updateByPrimaryKeySelective(user)>0?true:false;
    }

    @Override
    public boolean deleteUser(Integer id) {
        return userMapper.deleteByPrimaryKey(id)>0?true:false;
    }

    @Override
    public int deleteMany(String[] ids) {
        return userMapper.deleteMany(ids);
    }

    @Override
    public User login(String stuno, String pwd) {
        return userMapper.findByStunoAndPwd(stuno,pwd);
    }

    @Override
    public List<User> getActiveUsers(int limit) {
        return userMapper.selectActiveUsers(limit);
    }

    @Override
    public List<BigData> selectBigData() {
        return userMapper.selectBigData();
    }

}
